package com.xyb.controller;

import com.xyb.entity.UserInfo;
import com.xyb.entity.gitee.AuthRequestBody;
import com.xyb.entity.gitee.AuthResponseBody;
import com.xyb.entity.gitee.GiteeUserInfo;
import com.xyb.service.GiteeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("gitee")
@Slf4j
public class GiteeController {
    @Value("${oauth2.gitee.grantType}")
    private String grantType;
    @Value("${oauth2.gitee.clientId}")
    private String clientId;
    @Value("${oauth2.gitee.clientSecret}")
    private String clientSecret;
    @Value("${oauth2.gitee.redirectUrl}")
    private String redirectUrl;
    @Value("${oauth2.gitee.authServerUrl}")
    private String authServerUrl;
    @Value("${oauth2.gitee.tokenServerUrl}")
    private String tokenServerUrl;
    @Value("${oauth2.gitee.api.getUserInfoUrl}")
    private String getUserInfoUrl;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private GiteeService giteeService;

    @GetMapping("url")
    public String getAuthServerUrl(String state) {
        StringBuilder sb = new StringBuilder();
        sb.append(authServerUrl).append("?")
                .append("response_type").append("=").append("code")
                .append("&")
                .append("client_id").append("=").append(clientId)
                .append("&")
                .append("redirect_uri").append("=").append(redirectUrl)
                .append("&")
                .append("state").append("=").append(state);
        String url = sb.toString();
        log.info("gitee url: " + url);
        return url;
    }

    @GetMapping("info")
    public UserInfo code(String code, String state, HttpServletResponse response) throws IOException {
        log.info("gitee code: " + code);
        if (code == null || "".equals(code)) {
            return null;
        }
        // 发起 post 请求,请求 token
        AuthRequestBody authRequestBody = AuthRequestBody.builder()
                .grant_type(grantType)
                .client_id(clientId)
                .client_secret(clientSecret)
                .redirect_uri(redirectUrl)
                .code(code)
                .build();
        AuthResponseBody auth = restTemplate.postForObject(tokenServerUrl, authRequestBody, AuthResponseBody.class);

        log.info("gitee access_token: " + auth.getAccess_token());

        // 根据 access_token 调用 api
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", auth.getAccess_token());
        GiteeUserInfo giteeUserInfo = restTemplate.getForObject(getUserInfoUrl, GiteeUserInfo.class, paramMap);

        log.info("gitee user: " + giteeUserInfo.toString());
        UserInfo userInfo = giteeService.insertGiteeUser(giteeUserInfo);
        return userInfo;
    }
}
